/*
 * AboutPerson.cpp
 *
 *  Created on: 15.04.2010
 *      Author: king
 */

#include "AboutPerson.h"
#include <QFileDialog>
#include <QMessageBox>
#include "common/Date.h"
#include "genealogy_tree/Gender.h"
#include "genealogy_tree/Photo.h"

namespace etu {
namespace ui {

static const int SCALABILITY_COEFFICIENT = 150;

using etu::common::Date;
using etu::genealogy_tree::Gender;
using etu::genealogy_tree::Photo;

CAboutPerson::CAboutPerson(QWidget *parent)
    : QWidget(parent)
{
  setupUi(this);
  setMaximumWidth(minimumSizeHint().width());

  connect(m_changeFotoButton, SIGNAL(pressed()), this, SLOT(loadPhoto()));
  connect(m_applyButton, SIGNAL(pressed()), this, SLOT(apply()));
  connect(m_restoreButton, SIGNAL(pressed()), this, SLOT(showPersonData()));

  m_personWidget->m_deadCB->setVisible(true);
  this->setDisabled(true);
  m_isPhotoChanged = false;
}

void CAboutPerson::init(DataSource* dataSource)
{
  m_dataSource = dataSource;
  m_pixmap = new QGraphicsPixmapItem();
  m_photoScene = new QGraphicsScene();
  m_fotoView->setScene(m_photoScene);
  m_photoScene->addItem(m_pixmap);
}

void CAboutPerson::loadPhoto()
{
  QString fileName = QFileDialog::getOpenFileName(this, tr("Choose fhoto file"),"","Images (*.JPG *jpg *.tif *.png)");
  m_photoId = QString(fileName.toUtf8()).toStdString();
  if (m_photoId.empty())
	 return;
  m_isPhotoChanged = true;
  QPixmap origPixmap(QString::fromStdString(m_photoId));
  QPixmap* pixmap = new QPixmap(origPixmap.scaledToHeight(SCALABILITY_COEFFICIENT,Qt::SmoothTransformation));
  showPhoto(pixmap);
  emit updatePhoto(pixmap);
}

void CAboutPerson::showPhoto(QPixmap* pixmap)
{
  if (pixmap->isNull())
  {
	m_pixmap->setVisible(false);
	return;
  }
  m_pixmap->setVisible(true);
  m_pixmap->setPixmap(*pixmap);
  m_photoScene->setSceneRect(0,0,pixmap->width(),pixmap->height());
  m_fotoView->fitInView(m_pixmap, Qt::KeepAspectRatio);
}

void CAboutPerson::setPerson(Person* person, QPixmap* pixmap)
{
   setEnabled(true);
   m_person = person;
   showPersonData();
   showPhoto(pixmap);
}

void CAboutPerson::showPersonData()
{
   m_personWidget->setPerson(m_person);
   m_personWidget->m_maleRB->setChecked(m_person->getGender() == etu::genealogy_tree::MALE_GENDER);
   m_personWidget->m_femaleRB->setChecked(m_person->getGender() == etu::genealogy_tree::FEMALE_GENDER);
   m_personWidget->m_maleRB->setVisible(m_person->getGender() == etu::genealogy_tree::MALE_GENDER);
   m_personWidget->m_femaleRB->setVisible(m_person->getGender() == etu::genealogy_tree::FEMALE_GENDER);
   m_isPhotoChanged = false;
}

void CAboutPerson::apply()
{
   if(m_isPhotoChanged)
     updatePhoto();
   m_personWidget->getPerson();

   emit aboutPersonChange();
}

void CAboutPerson::updatePhoto()
{
  try
  {
    if (m_dataSource->isPhotoExists(*m_person))
	  m_dataSource->deletePhoto(*m_person);
    Photo photo(m_photoId);
    m_dataSource->addPhoto(m_person,photo);
    m_isPhotoChanged = false;
  }
  catch(etu::db::DataSourceException& ex)
  {
	 QMessageBox::critical(this,tr("ошибка записи фотографии в БД"),QString(ex.what()));
  }
}

}
}
